//
//  UserOpinionVControllerViewController.m
//  demoapp
//
//  Created by MingmingSun on 15/9/10.
//  Copyright (c) 2015年 pingplusplus. All rights reserved.
//

#import "UserOpinionVControllerViewController.h"
#import "BRPlaceholderTextView.h"

@interface UserOpinionVControllerViewController ()<UIAlertViewDelegate>

@property(nonatomic,weak) IBOutlet BRPlaceholderTextView *tview;
-(IBAction)buttonPressed:(id)sender;

@end

@implementation UserOpinionVControllerViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"意见反馈";
        self.navigationItem.leftBarButtonItem =
        [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow_white"]
                                         style:UIBarButtonItemStyleDone
                                        target:self
                                        action:@selector(backPressed:)];
    }
    return self;
}

-(void)backPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tview.placeholder=@"亲，您遇到什么技术问题或者有任何意见建议，欢迎您提给我们，谢谢！";
    self.tview.layer.borderColor=[UIColor black75PercentColor].CGColor;
//    [self.tview addMaxTextLengthWithMaxLength:0 andEvent:^(BRPlaceholderTextView *text) {
//        [self.view endEditing:YES];
//        UIAlertView * alter=[[UIAlertView alloc] initWithTitle:@"提示" message:@"超过指定长度了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//        [alter show];
//    }];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)buttonPressed:(id)sender{
    if(!self.tview.text.length)
        return;
    NSDictionary *para =
    @{@"title":@"用户意见",
      @"content":self.tview.text,
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"user_qa" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [self.navigationController popViewControllerAnimated:YES];
                }
                    break;
                case 1:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:@"评价成功" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
